package com.derbysoft.dswitch.adapter.ihg.common.service.dswitch2;

import com.derbysoft.dswitch.adapter.ihg.common.domain.FreeMealStrategy;
import com.derbysoft.dswitch.adapter.ihg.common.domain.HotelMapping;
import com.derbysoft.dswitch.adapter.ihg.common.service.AvailRoomStayEnhancer;
import com.derbysoft.dswitch.adapter.ihg.common.service.FreeMealMatchService;
import com.derbysoft.remote.dswitch.hotel.dto.AvailRoomStayDTO;
import com.derbysoft.remote.dswitch.hotel.dto.FreeMealDTO;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRoomStayDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: jason
 * Date: 2013-01-05
 */
@Component("ds2AvailRoomStayEnhancer")
public class DS2AvailRoomStayEnhancer implements AvailRoomStayEnhancer<HotelAvailRoomStayDTO> {

    @Autowired
    private FreeMealMatchService freeMealMatchService;

    @Override
    public void enhance(HotelAvailRoomStayDTO hotelAvailRoomStayDTO,
                        String channel, HotelMapping hotelMapping) {
        for (AvailRoomStayDTO availRoomStayDTO : hotelAvailRoomStayDTO.getRoomStays()) {
            String ratePlanCode = availRoomStayDTO.getRatePlan().getCode();
            String roomTypeCode = availRoomStayDTO.getRoomType().getCode();
            FreeMealStrategy freeMealStrategy =
                    freeMealMatchService.match(channel, hotelMapping.getCooperatorCode(), ratePlanCode, roomTypeCode);
            if (freeMealStrategy == null) {
                continue;
            }
            setFreeMeal(freeMealStrategy,availRoomStayDTO);
        }
    }

    private void setFreeMeal(FreeMealStrategy freeMealStrategy, AvailRoomStayDTO availRoomStayDTO) {
        FreeMealDTO freeMeal = new FreeMealDTO();
        freeMeal.setType(freeMealStrategy.getType());
        freeMeal.setDescription(freeMealStrategy.getDescription());
        freeMeal.setUnit(freeMealStrategy.getUnit());
        freeMeal.setQuantity(freeMealStrategy.getQuantity());
        availRoomStayDTO.getRatePlan().setFreeMeal(freeMeal);
    }
}
